﻿using Com.Ctrip.Framework.Apollo;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace MsgService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            string ip = string.Empty;
            string port = string.Empty;

            IConfigurationRoot config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddCommandLine(args)
                .Build();

            //var data = config["Data"];
            //两种方式读取
            //var defaultcon = config.GetConnectionString("DefaultConnection");
            //var devcon = config["ConnectionStrings:DevConnection"];

            ip = config["ip"];
            port = config["port"];

            if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))
            {
                ip = config.GetSection("host").GetValue<string>("ip", "");
                port = config.GetSection("host").GetValue<string>("port", "");
            }

            //IConfig config = ApolloConfigurationManager.GetAppConfig().Result;
            //ip = config.GetProperty("MSG-IP", "");
            //port = config.GetProperty("MSG-PORT", "");

            return WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, builder) =>
                {
                    builder.AddApollo(builder.Build().GetSection("apollo"))
                    .AddDefault()
                    .AddNamespace("TEST1.SHARE")
                    .AddNamespace("CLIENTSERVICE");
                })
                .UseStartup<Startup>()
                .UseUrls($"http://{ip}:{port}");
        }
    }
}
